using ETask.Api.Dtos;
using ETask.Api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ETask.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ProjectMemberController : ControllerBase
    {
        private readonly IProjectMemberService _projectMemberService;
        private readonly IAuditLogService _auditLogService;

        public ProjectMemberController(IProjectMemberService projectMemberService, IAuditLogService auditLogService)
        {
            _projectMemberService = projectMemberService;
            _auditLogService = auditLogService;
        }

        /// <summary>
        /// 添加项目成员
        /// </summary>
        /// <param name="request">添加成员请求</param>
        /// <returns></returns>
        [HttpPost("addMembers")]
        public async Task<IActionResult> AddProjectMembers([FromBody] AddProjectMembersRequest request)
        {
            try
            {
                var result = await _projectMemberService.AddProjectMembers(request.ProjectId, request.Members);
                
                // 记录添加项目成员日志
                if (result.Code == 200)
                {
                    var memberNames = string.Join(", ", request.Members.Select(m => m.Name));
                    await _auditLogService.LogSuccessAsync("添加", "项目成员", $"添加项目成员: {memberNames}", request.ProjectId);
                }
                else
                {
                    await _auditLogService.LogErrorAsync("添加", "项目成员", "添加项目成员失败", result.Message, request.ProjectId);
                }
                
                return Ok(result);
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("添加", "项目成员", "添加项目成员异常", ex.Message, request.ProjectId);
                return Ok(ApiResponse.Fail(500, $"添加项目成员失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 批量添加项目成员
        /// </summary>
        /// <param name="request">批量添加成员请求</param>
        /// <returns></returns>
        [HttpPost("batchAddMembers")]
        public async Task<IActionResult> BatchAddProjectMembers([FromBody] BatchAddProjectMembersRequest request)
        {
            try
            {
                var result = await _projectMemberService.BatchAddProjectMembers(request.ProjectId, request.UserIds, request.Role);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return Ok(ApiResponse.Fail(500, $"批量添加项目成员失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 删除项目成员
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        [HttpPost("deleteMember")]
        public async Task<IActionResult> DeleteProjectMember([FromBody] PrimaryKey key)
        {
            try
            {
                var result = await _projectMemberService.DeleteProjectMember(key.Id);
                
                // 记录删除项目成员日志
                if (result.Code == 200)
                {
                    await _auditLogService.LogSuccessAsync("删除", "项目成员", "删除项目成员", key.Id);
                }
                else
                {
                    await _auditLogService.LogErrorAsync("删除", "项目成员", "删除项目成员失败", result.Message, key.Id);
                }
                
                return Ok(result);
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("删除", "项目成员", "删除项目成员异常", ex.Message, key.Id);
                return Ok(ApiResponse.Fail(500, $"删除项目成员失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 更新项目成员信息
        /// </summary>
        /// <param name="memberDto">成员信息</param>
        /// <returns></returns>
        [HttpPost("updateMember")]
        public async Task<IActionResult> UpdateProjectMember([FromBody] ProjectMemberDto memberDto)
        {
            try
            {
                var result = await _projectMemberService.UpdateProjectMember(memberDto);
                
                // 记录更新项目成员日志
                if (result.Code == 200)
                {
                    await _auditLogService.LogSuccessAsync("修改", "项目成员", $"修改项目成员信息", memberDto.Id, memberDto.Name);
                }
                else
                {
                    await _auditLogService.LogErrorAsync("修改", "项目成员", "修改项目成员失败", result.Message, memberDto.Id, memberDto.Name);
                }
                
                return Ok(result);
            }
            catch (Exception ex)
            {
                await _auditLogService.LogErrorAsync("修改", "项目成员", "修改项目成员异常", ex.Message, memberDto.Id, memberDto.Name);
                return Ok(ApiResponse.Fail(500, $"更新项目成员失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 获取项目成员列表
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <returns></returns>
        [HttpPost("getMembers")]
        public async Task<IActionResult> GetProjectMembers([FromBody] PrimaryKey key)
        {
            try
            {
                var result = await _projectMemberService.GetProjectMembers(key.Id);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return Ok(ApiResponse.Fail(500, $"获取项目成员失败: {ex.Message}"));
            }
        }
    }
}
